﻿using System;

using System.Collections.Generic;
using System.IO;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Animations;
using System.Drawing;
using System.Xml;
using MelectricComponents;

namespace FileExplorer
{
    public class AddPetrolWindow : Window
    {
        private Toolbar toolbar;
        private BackgroundDecorator background;
        TextBox txtLitres;
        TextBox txtDateTime;
        TextBox txtMiles;
        TextBox txtCost;
        Label lblAttribute;

        public AddPetrolWindow()
            : base()
        {
            this.Flags = ControlFlags.IgnoreChildClipping;
            background = new BackgroundDecorator { Color = System.Drawing.Color.Silver };
            background.Margin = new Margin(0);
            Controls.Add(background);

            txtDateTime = new TextBox
            {
                Alpha = 128
               ,
                ToolTip = new DatePickerToolTip()
               ,
                Size = new Size(Screen.Width, 100)
               ,
                Location = new Point(0, 0)
               ,
                Text = DateTime.Now.ToString("dd/MM/yyyy")
            };
            Controls.Add(txtDateTime);
            txtLitres = new TextBox
                                    {
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8 * 5
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                        ,
                                        Location = new Point(0, 100)
                                    };
            Controls.Add(txtLitres);
            lblAttribute = new Label
            {
                Text = "Litres"
                ,
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 3
                ,
                Location = new Point(Screen.Size.Width / 8 * 5, 100)
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
            };
            Controls.Add(lblAttribute);
            txtMiles = new TextBox
            {
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 5
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                ,
                Location = new Point(0, 200)
            };
            Controls.Add(txtMiles);
            lblAttribute = new Label
            {
                Text = "Miles"
                ,
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 3
                ,
                Location = new Point(Screen.Size.Width / 8 * 5, 200)
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
            };
            Controls.Add(lblAttribute);
            txtCost = new TextBox
            {
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 5
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                ,
                Location = new Point(0, 300)
            };
            Controls.Add(txtCost);
            lblAttribute = new Label
            {
                Text = "Cost"
                ,
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8 * 3
                ,
                Location = new Point(Screen.Size.Width / 8 * 5, 300)
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
            };
            Controls.Add(lblAttribute);
 
            toolbar = new Toolbar();
            Toolbar = toolbar;
            toolbar.LeftButton.Text = "Cancel";
            toolbar.RightButton.Text = "Submit";
            toolbar.LeftButton.Click += (s, e) => { Hide(TransitionMode.ZoomOut); };
            toolbar.RightButton.Click += (s, e) => { SaveData(); };
        }


        private void SaveData()
        {
            Dictionary<string, string> Attributes = new Dictionary<string, string>();
            Attributes.Add("DateTime", txtDateTime.Text);
            Attributes.Add("Litres", txtLitres.Text);
            Attributes.Add("Miles", txtMiles.Text);
            Attributes.Add("Cost", txtCost.Text);
            XMLUtility oXMLUtility = new XMLUtility();
            oXMLUtility.SaveXML("Petrol", "FillUp", Attributes);

            string sLitres = txtLitres.Text;
            double litresPerMile = Convert.ToDouble(txtMiles.Text)/Convert.ToDouble(txtLitres.Text);
            double MilesPerGallon = litresPerMile * 4.54609188;

            txtLitres.Text = "";
            txtMiles.Text = "";
            txtCost.Text = "";




            ConfirmDialog dlg = new ConfirmDialog("Petrol Topped Up", sLitres + " Litres of petrol added to tank." + Environment.NewLine + MilesPerGallon + " Miles Per Gallon", true);
            dlg.Margin = new Margin(0, null, 0, 0);
            dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
            {
                Hide(TransitionMode.ZoomOut);
            });
        }
    }
}
